package com.soap.desensitize.util;

import com.soap.desensitize.encry.IDesensitization;
import com.soap.desensitize.exception.strategy.DecodeException;
import com.soap.desensitize.exception.strategy.EncodeException;
import com.soap.desensitize.strategy.json.JsonListStrategy;
import com.soap.desensitize.strategy.json.JsonMapStrategy;
import com.soap.desensitize.strategy.json.JsonObjStrategy;

import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @desc：
 * @author： QGP
 * @create： 2022/5/2 14:32
 */
public class JsonUtils {

    /**
     * @param data 需要加密的值
     * @param encry      加密策略
     * @return
     * @throws NoSuchAlgorithmException
     * @throws EncodeException
     * @desc 判断类型并且加密
     */
    public static Object judgeAndEncode(Object data, IDesensitization encry) {
        Object str = null;

        try {
            switch (data.getClass().getName()) {
                case "java.lang.Integer":
                    str = encry.encode(data + "");
                    break;
                case "java.lang.Long":
                    str = encry.encode(data + "");
                    break;
                case "java.lang.Float":
                    str = encry.encode(data + "");
                    break;
                case "java.lang.Double":
                    str = encry.encode(data + "");
                    break;
                case "java.lang.Boolean":
                    str = encry.encode(data + "");
                    break;
                case "java.lang.Date":
                    str = encry.encode(data + "");
                    break;
                case "java.lang.String":
                    str = encry.encode(data);
                    break;
                case "java.util.List":
                case "java.util.ArrayList":
                    return new JsonListStrategy(encry).encode((List) data);
                case "java.util.Map":
                case "java.util.HashMap":
                    return JsonMapStrategy.getInstance(encry).encode((Map<?, ?>) data);
                default:
                    return new JsonObjStrategy(encry).encode(data);
            }
        } catch (EncodeException e) {
            throw new RuntimeException(e);
        }
        return str;
    }


    /**
     * @param data 需要解密的值
     * @param encry      加密策略
     * @return
     * @throws NoSuchAlgorithmException
     * @throws EncodeException
     * @desc 判断类型并且加密
     */
    public static Object judgeAndDecode(Object data, IDesensitization encry) throws DecodeException {
        Object str = null;

        try {
            switch (data.getClass().getName()) {
                case "java.lang.Integer":
                    str = encry.decode(data + "");
                    break;
                case "java.lang.Long":
                    str = encry.decode(data + "");
                    break;
                case "java.lang.Float":
                    str = encry.decode(data + "");
                    break;
                case "java.lang.Double":
                    str = encry.decode(data + "");
                    break;
                case "java.lang.Boolean":
                    str = encry.decode(data + "");
                    break;
                case "java.lang.Date":
                    str = encry.decode(data + "");
                    break;
                case "java.lang.String":
                    str = encry.decode(data);
                    break;
                case "java.util.List":
                case "java.util.ArrayList":
                    return new JsonListStrategy(encry).decode(data.toString());
                case "java.util.Map":
                case "java.util.HashMap":
                case "java.util.LinkedHashMap":
                    return JsonMapStrategy.getInstance(encry).decode(data.toString());
                default:
                    return new JsonObjStrategy(encry).decode(data.toString());
            }
        } catch (DecodeException e) {
            throw new DecodeException("Json解密异常");
        }

        return str;
    }


}
